//
//  ShareTool.m
//  ShareItemWX
//
//  Created by fujinliang on 17/1/9.
//  Copyright © 2017年 星匠. All rights reserved.
//

#import "ShareTool.h"
#import "HBShareBaseActivity.h"
#import "AppDelegate.h"

@interface ShareTool()
@property (nonatomic, copy) UIActivityViewControllerCompletionHandler completionHandler;
@end
@implementation ShareTool
- (void)shareWithTitle:(NSString *)title description:(NSString *)description url:(NSString *)url image:(UIImage *)image completionHandler:(UIActivityViewControllerCompletionHandler)completionHandler
{
    NSMutableArray *items = [[NSMutableArray alloc] init];
    [items addObject:title?:@""];
    if (image) {
        [items addObject:image];
    }
    if (url) {
        [items addObject:url];
    }
    
    NSMutableArray *activities = [[NSMutableArray alloc] init];
    HBShareBaseActivity *weixinActivity = [[HBShareBaseActivity alloc] initWithTitle:@"微信" type:ActivityServiceWeixin];
    HBShareBaseActivity *weixinFriendsActivity = [[HBShareBaseActivity alloc] initWithTitle:@"朋友圈" type:ActivityServiceWeixinFriends];
    [@[weixinActivity, weixinFriendsActivity] enumerateObjectsUsingBlock:^(HBShareBaseActivity *activity, NSUInteger idx, BOOL *stop) {
        activity.urlString = url;
        activity.shareDescription = description;
        activity.shareTitle = title;
        activity.image = image;
    }];
    [activities addObjectsFromArray:@[weixinActivity, weixinFriendsActivity]];
    
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:activities];
    NSMutableArray *excludedActivityTypes =  [NSMutableArray arrayWithArray:@[UIActivityTypeAirDrop, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypeMail, UIActivityTypePostToTencentWeibo, UIActivityTypeSaveToCameraRoll, UIActivityTypeMessage, UIActivityTypePostToTwitter]];
    
    activityViewController.excludedActivityTypes = excludedActivityTypes;
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.window.rootViewController presentViewController:activityViewController animated:YES completion:nil];
    
    activityViewController.completionHandler = ^(NSString *activityType, BOOL complted){
        if (completionHandler) {
            completionHandler(activityType, complted);
            self.completionHandler = nil;
        }
    };
}
@end
